package com.smec.remes.module.user.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.smec.moblie.R;
import com.smec.moblie.databinding.FragmentStationBinding;
import com.smec.remes.base.SmecBaseFragment;
import com.smec.remes.bean.AbleMatnr;
import com.smec.remes.bean.GetUserName;
import com.smec.remes.module.request.SmecRequestPresenter;
import com.smec.remes.module.user.adapter.SmecUserAdapter;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.WmsConstant;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/5.
 */

public class SmecStationFragment extends SmecBaseFragment<SmecRequestPresenter>{
    private FragmentStationBinding fragmentStationBinding;
    private SmecUserAdapter smecUserAdapter;
    public final static  String USERNAME="USERNAME";
    public final static  String USERNAMEFAIL="USERNAMEFAIL";
    private Handler mHandler=new Handler();
    private int pageNumber=1;
    private String  status;
    private String condition="";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentStationBinding=DataBindingUtil.inflate(inflater, R.layout.fragment_station,container,false);
        smecUserAdapter=new SmecUserAdapter(getActivity());
        fragmentStationBinding.userRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        fragmentStationBinding.userRecyclerView.setAdapter(smecUserAdapter);
        fragmentStationBinding.edSearch.clearFocus();
        _HideKeyboard();
        _setEditext();
        _search();
        _delete();
        _getMsg();
        return fragmentStationBinding.getRoot();
    }

    public void _getMsg(){
        fragmentStationBinding.ptr.setHeaderView(WmsConstant.getSinaRefreshView());
        fragmentStationBinding.ptr.setBottomView(new LoadingView(getActivity()));
        fragmentStationBinding.ptr.setAutoLoadMore(true);
        fragmentStationBinding.ptr.setEnableOverScroll(false);
        fragmentStationBinding.ptr.setEnableLoadmore(true);
        fragmentStationBinding.ptr.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                pageNumber=1;
                status="onRefresh";
                mPresenter.getUsername(condition, "STATION", pageNumber);
                pageNumber += 1;
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                status="onLoadMore";
                mPresenter.getUsername(condition,"STATION",pageNumber);
                pageNumber+=1;
                pageNumber+=1;
            }
        });
        fragmentStationBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                fragmentStationBinding.ptr.startRefresh();
            }
        },500);


    }

    public void _setEditext(){
        fragmentStationBinding.edSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                fragmentStationBinding.ivDelete.setVisibility(View.VISIBLE);
                fragmentStationBinding.ivSearch.setImageResource(R.drawable.smec_remes_lightsearch);
            }
        });
    }

    public void _HideKeyboard(){
        fragmentStationBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                CommonUtils.hideImmManager(fragmentStationBinding.getRoot());
            }
        },100);
    }

    public void _delete(){
        fragmentStationBinding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                condition="";
                fragmentStationBinding.edSearch.setText("");
            }
        });
    }

    public static  SmecStationFragment newInstance(){
        Bundle args = new Bundle();
        SmecStationFragment smecStationFragment=new SmecStationFragment();
        smecStationFragment.setArguments(args);
        return smecStationFragment;
    }

    public void _search(){
        fragmentStationBinding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchBykeyWord();
            }
        });
        fragmentStationBinding.edSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    searchBykeyWord();
                }
                return false;
            }
        });
    }

    private void searchBykeyWord(){
        if (!TextUtils.isEmpty(fragmentStationBinding.edSearch.getText().toString())){
            condition=fragmentStationBinding.edSearch.getText().toString();
        }else {
            condition="";
        }
        fragmentStationBinding.ptr.startRefresh();
        CommonUtils.hideImmManager(fragmentStationBinding.getRoot());
    }

    @Override
    protected SmecRequestPresenter getPersenter() {
        return new SmecRequestPresenter(getActivity());
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(USERNAME)
            }
    )

    public void searchUser(ArrayList<GetUserName> arrayList){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(status.equals("onRefresh")){
                    fragmentStationBinding.ptr.finishRefreshing();
                }else {
                    fragmentStationBinding.ptr.finishLoadmore();
                }
            }
        },200);
        if(CommonUtils.notEmpty(arrayList)){
            smecUserAdapter._addList(arrayList,status);
        }else {
            if(status.equals("onRefresh")){
                smecUserAdapter._clearList();
            }
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(USERNAMEFAIL)
            }
    )

    public void searchFail(String a){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(status.equals("onRefresh")){
                    fragmentStationBinding.ptr.finishRefreshing();
                }else {
                    fragmentStationBinding.ptr.finishLoadmore();
                }
            }
        },200);
    }
}
